<template>
  <a-spin :spinning="loading">
    <div class="modal-header">
      <div class="modal-title">{{ l('EditTenantFeatures') }}</div>
    </div>
    <div class="modal-body">
      <div v-if="featureTree.editData.features">
        <edition-feature-tree
          :editData="featureTree.editData"
          ref="featureTree"
        ></edition-feature-tree>
      </div>
    </div>
    <div class="modal-footer">
      <a-button :disabled="saving" :loading="saving" @click="resetFeatures()" type="button">
        {{ l('ResetSpecialFeatures') }}
      </a-button>
      <a-button :disabled="saving" @click="close()" type="button">
        {{ l('Cancel') }}
      </a-button>
      <a-button :loading="saving" :type="'primary'" @click="save()">
        <i class="acticon acticon-save"></i>
        <span>{{ l('Save') }}</span>
      </a-button>
    </div>
  </a-spin>
</template>

<script lang="ts">
  import { ModalComponentBase } from '/@/shared/component-base';
  import { EditionFeatureTree } from '/@/app/admin-shared';
  import {
    TenantServiceProxy,
    UpdateTenantFeaturesInput,
    EntityDto,
    GetTenantFeaturesEditOutput,
  } from '/@/shared/service-proxies';
  import { defineComponent } from 'vue';
  export default defineComponent({
    components: { EditionFeatureTree },
    mixins: [ModalComponentBase],
    props: {
      tenantId: {
        type: Number, // 实际传过来的类型
      },
    },
    data() {
      return {
        TenantServiceProxy: new TenantServiceProxy(),
        featureTree: { editData: new GetTenantFeaturesEditOutput() },
        resettingFeatures: false,
      };
    },
    mounted() {
      this.getData();
    },
    methods: {
      /**
       * 获取数据
       */
      getData() {
        this.loading = true;
        this.TenantServiceProxy.getTenantFeaturesForEdit(this.tenantId)
          .finally(() => {
            this.loading = false;
          })
          .then((result) => {
            this.featureTree.editData = result;
          });
      },
      /** 保存 */
      save() {
        if (!(this.$refs.featureTree as any).areAllValuesValid()) {
          this.message.warn(this.l('InvalidFeaturesWarning'));
          return;
        }
        const input = new UpdateTenantFeaturesInput();
        input.id = this.tenantId as any;
        input.featureValues = (this.$refs.featureTree as any).getGrantedFeatures();
        this.saving = true;
        this.TenantServiceProxy.updateTenantFeatures(input)
          .finally(() => (this.saving = false))
          .then(() => {
            this.notify.success(this.l('SavedSuccessfully'));
            this.success(true);
          });
      },
      /** 重置特殊的特性参数 */
      resetFeatures() {
        const input = new EntityDto();
        input.id = this.tenantId as any;
        this.resettingFeatures = true;
        this.TenantServiceProxy.resetTenantSpecificFeatures(input)
          .finally(() => (this.saving = false))
          .then(() => {
            this.notify.success(this.l('ResetSuccessfully'));
            this.getData();
          });
      },
    },
  });
</script>

<style lang="less" scoped></style>
